<?php
require_once ('../lib/config.php');
require_once ('../lib/facebook.php');
require_once ('../lib/friends.php');
require_once ('../lib/friend.php');
require_once ('../lib/test.php');
require_once ('../../simpletest/autorun.php');
require_once ('../lib/init.php');

class TestFunc_callMultiFQL extends UnitTestCase {
    
    function setUp(){
        global $login_button,$facebook;
        $user = $facebook->getUser();
        if(!$user){
            try{
                $me = getOneself();
            }catch(Exception $e){
                print($login_button);
                exit();
            }
        }
    }
    
    //refer to http://www.simpletest.org/en/unit_test_documentation.html
    function testCallMultiQuery(){ 
    
        $queries = array(
            "query1" => "select uid2 from friend where uid1 = me()",
            "query2" => "select name from user where uid IN (select uid2 from #query1)"
        );
        
        $response = callMultiFQL($queries);

        $this->assertNotNull($response);
        $this->assertTrue(is_array($response));
        
        $queries = null;
        $response = callMultiFQL($queries);
        $this->assertNull($response);
        
        $queries = "";
        $response = callMultiFQL($queries);
        $this->assertNull($response);
        
        $queries = array(
            "query1" => "select 1 from friend where uid1 = me()",
            "query2" => "select name from 1 where uid IN (select uid2 from #query1)"
        );
        try{
            $response = callMultiFQL($queries);
            $this->assertTrue(false);
        }catch(Exception $e){
            $this->assertTrue(true);
        }
        

    }
    
    function testGetMessage(){
        //Queries
        $threadQuery = "SELECT thread_id, recipients, updated_time FROM thread 
        				WHERE folder_id = 0 OR folder_id = 1 AND updated_time > ".QUERY_SINCE;
        
        $messageQuery = "SELECT thread_id, author_id, body FROM message 
        				WHERE thread_id IN (
                        	SELECT thread_id FROM #threadQuery
                    	) AND created_time > ".QUERY_SINCE;
        
        $queries = array();
        $queries['threadQuery'] = $threadQuery;
        $queries['messageQuery'] = $messageQuery;
        
        $data = callMultiFQL($queries);
       
//        print_r($data);
    }
}
?>
